from django.test import TestCase
import jwt  # 导包
import datetime

# 定义 jwt令牌携带的信息
dic = {
    "exp": datetime.datetime.now() + datetime.timedelta(hours=1),  # 设置到期时间
    "iat": datetime.datetime.now(),  # 设置生成令牌的时间
    "data": {  # 我们自定义携带是用户数据
        "uid": 10,
        "username": "张三"
    }
}

# 生成一个jwt令牌token   参数1 令牌携带信息  key 加密算法 进行加密的秘钥 algorithm加密算法
s = jwt.encode(dic, key="abcdef", algorithm="HS256")
print(s)#eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTM0NDUyMDAsImlhdCI6MTU5MzQ0MTYwMCwiZGF0YSI6eyJ1aWQiOjEwLCJ1c2VybmFtZSI6Ilx1NWYyMFx1NGUwOSJ9fQ.kM_uoI6xAf4HAUMLIgbiZYJXYGzxTxsNDIgaRyZ7fFI
# s = s + b"123"
# 我们解析令牌token
r = jwt.decode(s, key="abcdef", algorithms=["HS256"])
print(r)  # {'exp': 1593445200, 'iat': 1593441600, 'data': {'uid': 10, 'username': '张三'}}
print(type(r))  # <class 'dict'>


print("Hello Goods")